package com.hoshiibuy.platform.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.common.utils.BeanUtils;
import com.hoshiibuy.platform.entity.PlatUsersRole;
import com.hoshiibuy.platform.entity.param.PlatUsersRoleInsertParam;
import com.hoshiibuy.platform.entity.param.PlatUsersRoleParam;
import com.hoshiibuy.platform.entity.param.PlatUsersRoleUpdateParam;
import com.hoshiibuy.platform.service.PlatUsersRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 用户角色关联表 前端控制器
 * </p>
 *
 * @author yl
 * @since 2019-04-20
 */
@Controller
@RequestMapping("/platUsersRole")
@Api(value = "平台用户角色管理", tags = {"平台用户角色信息操作接口"})
@Slf4j
public class PlatUsersRoleController {

    @Autowired
    private PlatUsersRoleService platUsersRoleService;

    /**
     * @param param
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 分页查询平台信息
     */
    @ApiOperation(value = "分页查询平台记录")
    @PostMapping("/query/page")
    public Response page(@RequestBody @Valid PlatUsersRoleParam param) {
        try {

            QueryWrapper<PlatUsersRole> queryWrapper = new QueryWrapper<>();

            IPage<PlatUsersRole> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));

            return Response.data(platUsersRoleService.page(page, queryWrapper));

        } catch (ServiceException e) {
            log.error("分页查询平台用户角色信息异常", e);
            return Response.failed(e.getError().getMsg());

        } catch (Exception e) {
            log.error("分页查询平台用户角色信息异常", e);
            return Response.failed("分页查询平台用户角色信息异常");

        }

    }

    /**
     * @param [id]
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据id查询
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询平台用户角色信息")
    public Response queryPlatformById(Long id) {
        return Response.data(platUsersRoleService.getById(id));
    }

    /**
     * @param [userId]
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 根据用户id查询
     */
    @GetMapping("/query")
    @ApiOperation(value = "查询平台用户角色信息")
    public Response queryPlatformByName(String userId) {

        Wrapper wrapper = Wrappers.<PlatUsersRole>query()
                .lambda().eq(PlatUsersRole::getUserId, userId);

        return Response.data(platUsersRoleService.getOne(wrapper));

    }

    /**
     * @param [platUsersRoleInsertParam]
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台用户角色信息")
    public Response savePlatform(@Valid PlatUsersRoleInsertParam platUsersRoleInsertParam) {

        try {
            PlatUsersRole platUsersRole = BeanUtils.covertType(platUsersRoleInsertParam, PlatUsersRole.class);
            return Response.data(platUsersRoleService.save(platUsersRole));
        } catch (Exception e) {
            log.error("", e);
            return Response.failed();
        }

    }

    /**
     * @param [platUsersRole]
     * @return com.hoshiibuy.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台用户角色信息")
    public Response updatePlatformById(@Valid PlatUsersRoleUpdateParam platUsersRoleUpdateParam) {

        try {
            PlatUsersRole platUsersRole = BeanUtils.covertType(platUsersRoleUpdateParam, PlatUsersRole.class);
            return Response.data(platUsersRoleService.updateById(platUsersRole));
        } catch (Exception e) {
            log.error("", e);
            return Response.failed();
        }


    }
}

